﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using CoolEngine.Engine;
using CoolEngine.Gui;
using Microsoft.Xna.Framework.Graphics;
using CoolEngine.Engine.Input;
using CoolEngine.Engine.Scripting;
using CoolEngine.Engine.Graphics;

namespace CoolEngine
{
    public class CoolGame:Game
   { 
        public GraphicsDeviceManager Graphics { get; private set; }
        public GuiRenderer GuiRenderer { get; private set; }
        public SpriteBatch Batch { get; private set; }
        public Camera Camera { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "CoolEngine.Engine.WorldInterface"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "CoolEngine.Engine.ConsoleWindow")]
        public CoolGame():base()
        {            
            GameManager.Game = this;
            Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = @".\Content";
            ScanForBoundItems();
            SetUpRenderer();
            SetUpGameMenuControl();
            SetUpCamera();
            SetUpScripting();
            ConsoleWindow console = new ConsoleWindow(this, GuiRenderer);
            GameManager.GameState = new GameState(this);
            
            WorldInterface renderer = new WorldInterface(GameManager.Camera,this, GuiRenderer);
            GameManager.GameState.Screens.Push(renderer);
            GameManager.GuiRenderer.RenderGameScreenOnly = true;
            
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        private void SetUpScripting()
        {
            ScriptManager.SetUpScripts(this);            
        }


        private void SetUpCamera()
        {
            Camera = new Camera(this);
            GameManager.Camera = Camera;
        }

        private static void ScanForBoundItems()
        {
            HardCodedCommandParser.ScanForBoundCommands();
            Variables.ScanForBoundVariables();
            ScriptManager.SearchForBoundFunctions();
        }

        private void SetUpGameMenuControl()
        {
            GameMenuControl gameMenu = new GameMenuControl(this, GuiRenderer);
            //Components.Add(gameMenu);
            Services.AddService(typeof(GameMenuControl), gameMenu);
        }

        private void SetUpRenderer()
        {
            GuiRenderer = new GuiRenderer(this, false);
            GameManager.GuiRenderer = GuiRenderer;
            Components.Add(GameManager.GuiRenderer);            
            Services.AddService(typeof(GuiRenderer), GuiRenderer);
        }

        protected override void Initialize()
        {
            Batch = new SpriteBatch(GraphicsDevice);
            base.Initialize();
        }
        protected override void Update(GameTime gameTime)
        {
            InputHandler.HandleInput(PlayerIndex.One);
            base.Update(gameTime);
        }
    }
}
